using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FeatureWally.Client.UI.Controls {
    public class FWNodeFeatureError : TreeNode {

        private FWFeatureDefinition _definition;

        public FWNodeFeatureError(FWFeatureDefinition definition)
            : base(definition.DisplayName, 7, 7) {
            _definition = definition;
            base.Tag = _definition.Definition;
        }

        private string _dummyPath = string.Empty;
        private string _dummyFile = string.Empty;
        private bool _dummyFileCreated = false;

        public bool DummyFileCreated {
            get { return _dummyFileCreated; }
        }
        public FWFeatureDefinition Definition {
            get { return _definition; }
        }

        public void CreateDummyDefinitionFile() {
            try {
                _dummyPath = _definition.Definition.RootDirectory;

                if (!Directory.Exists(_dummyPath)) {
                    Directory.CreateDirectory(_dummyPath);
                }
                _dummyFile = _dummyPath + "\\feature.xml";

                StringBuilder xml = new StringBuilder();
                xml.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                xml.AppendFormat("<Feature Scope=\"Site\" Id=\"{0}\" xmlns=\"http://schemas.microsoft.com/sharepoint/\">", _definition.Definition.Id.ToString("N"));
	            xml.Append("<ElementManifests/>");
                xml.Append("</Feature>");
                using (StreamWriter writer = File.CreateText(_dummyFile)) {
                    writer.Write(xml.ToString());
                }
                _dummyFileCreated = true;
            }
            catch (Exception exc) {
                throw new Exception("An error occurred while saving dummy definition file", exc);
            }
        }

        public void RemoveDummyDefinitionFile() {
            if (Directory.Exists(_dummyPath)) {
                Directory.Delete(_dummyPath, true);
            }
        }

        public string GetStsAdm(bool includeHeader, bool activate, bool uninstall) {
            StringBuilder result = new StringBuilder();

            if (includeHeader) {
                result.Append("REM Autogenerated STSADM commands to deactivate and uninstall features");
                result.Append("\r\nREM Copy-Paste this to a .bat file to manually execute it.");
                result.Append("\r\nREM Generated by Feature Wally");
                result.AppendFormat("\r\n\r\n@SET STSADM=\"{0}\"", Properties.Settings.Default.STSADM);
            }

            result.AppendFormat("\r\n\r\nREM Feature: {0}", _definition.DisplayName);
           
            if (uninstall) {
                result.AppendFormat("\r\n%STSADM% -o uninstallfeature -name {0}", _definition.DisplayName);
            }

            return result.ToString();
        }
    }
}
